local Entity = require'entities.entity'

local WorkStation = {}
WorkStation.mt = { __index = WorkStation }
setmetatable( WorkStation, Entity.mt)

-- to add a new workstation:
-- create a workstation subclass
-- follow the model from other workstations
-- add resource definition
-- edit config.lua with workstation constant
-- edit workstation.place below
-- edit Game:isWorkstation and Game:resource_from_number
-- edit Construct:place_station and Construct:station_num_to_suffix
-- add to entity.deck_tidy
-- add to ship layout
-- add tubing
-- add to construct:make_network_map




function WorkStation.initialize( type, x, y, health, deck, ship, resources, requires )
	local dt, tt
	
	if type then tt = type else tt = 'workstation' end	
	
	local wk = Entity.new( tt, deck )
	setmetatable(wk, WorkStation.mt)
	
	if resources then wk.res = resources else wk.res = {} end
	if requires then wk.req = requires else wk.req = {} end
	
	

	wk.health = health or 100
	wk.prop:setPriority( 52 )
	wk.gui_open = false
	wk.gui_made = false
	wk.gui = {}
	wk.dmg_rate = BASE_DAMAGE_RATE/BASE_DAMAGE_RATE
	wk.powered = true
	
	if x and y then wk.prop:setLoc(x, y) end
	if ship then 
		ship.layer:insertProp(wk.prop);
		wk.prop:setAttrLink(MOAITransform.INHERIT_TRANSFORM, 
			ship.transform, MOAITransform.TRANSFORM_TRAIT)
		wk.ship = ship
	end
	
	
	--print("new workstation")
	
	return wk

end

function WorkStation.new(type, x, y, health, deck, ship, resources, requires)

	local o = WorkStation.initialize(type, x, y, health, deck, ship, resources, requires)
	
	return o

end

function WorkStation.place( type, x, y, ship )

	local stations = {
		[CONTROL_PANEL] = function(x, y) return ControlPanel.new(x, y, ship) end,
		[FUEL_CELL] = function(x, y) return FuelCell.new(x, y, ship) end,
		[SPACE_TOILET] = function(x, y) return SpaceToilet.new(x, y, ship) end,
		[WATER_TANKS] = function(x, y) return WaterTanks.new(x, y, ship) end,
		[OXYGEN_TANKS] = function(x, y) return OxygenTanks.new(x, y, ship) end,
		[RECYCLER] = function(x, y) return Recycler.new(x, y, ship) end,
		[FOOD_STATION] = function(x, y) return FoodStation.new(x,y, ship) end,
		[SLEEP_TUBE] = function(x, y) return SleepTube.new(x,y, ship) end,
		
	
	}
	
	return stations[type](x,y, ship)

end


function WorkStation:take_damage(damage)
	
	local dr = self.damage_rate
	local d = damage
	local d_mod = BASE_DAMAGE_MOD
	local dm = (dr - ((dr + d) / 2))/d_mod
	
	
	--local dm = compute_dm(self.dmg_rate, 
	--	damage, BASE_DAMAGE_MOD)

end

function WorkStation:update(dt)

	local work_upd = function(self, dt)
		local x, y = self.prop:getWorldLoc()
		self.window_x = math.floor(x-TILE_SIZE/2)
		self.window_y = math.floor(y-TILE_SIZE/2)
		
		if self.res then
		
			WorkStation.update_resources(self, dt)
		
		end
		
		WorkStation.check_electric(self)
		
	end
	
	local a = MOAICoroutine.new()
	a:run(work_upd, self, dt)
	
	--print(x,y)
	--self.gui:setPos(

end

function WorkStation:check_electric()

	local work_check_elec = function(self)
		if self.type == 'fuel_cell' then
		
			FuelCell.test_connections(self)
			
			
		else
			local a
			if self.powered == false then
				if self.res[RES_POWER] then
					self.res[RES_POWER].rate = self.res[RES_POWER].base_rate
				end
			
			elseif self.powered == true then
				if self.res[RES_POWER] then
					if self.res[RES_POWER].pow_rate then
				
						self.res[RES_POWER].rate = self.res[RES_POWER].pow_rate
						local a
					end
				end
			end
		end
	
	end
	
	
	
	
	local a = MOAICoroutine.new()
	
	a:run(work_check_elec, self)
	

end

function WorkStation:inside(x, y, z, pad)

	if not z then z = 0 end	
	
	local x1, y1 = self.prop:getWorldLoc()
	x1 = math.floor(x1-TILE_SIZE/2)
	y1 = math.floor(y1-TILE_SIZE/2)
	
	
	
	local x5, y5 = self.ship.layer:wndToWorld(x, y)
	x5 = math.floor(x5)
	y5 = math.floor(y5)
	
	
	
	local x2, y2 = x1+TILE_SIZE, y1+TILE_SIZE
	
	if x5 >= x1 and x5 <= x2 and y5 >= y1 and y5 <= y2 then
		
		--print("INSIDE .. " .. self.type)
		return true
	
	end

	

end

function WorkStation:open_gui()
	print("WORKSTATION OPEN GUI")
	self.gui_open = true
	
	local res = self.res
	
	local sp = " " .. self.type
	
	if res then
		for i = 1, #res do
			
			local s = " " .. res[i].name .. " " .. res[i].current
			sp = sp .. s
		
		end
		
		if self.gui_made then self.gui:setText(sp) end
		
	end
	
	Game.selected_construct = self.ship
	if not self.gui_made then
		self.gui_made = true
		self.gui, self.gui_id = GuiManager:new_popup("window", 0, 87.5, 50-6.75, 12.5, sp)
		self.gui:setBackgroundImage("assets\\8metal.png")
		self.gui:setTextAlignment(self.gui.TEXT_ALIGN_LEFT, self.gui.TEXT_ALIGN_TOP)
		local a
	else
		self:gui_show()
	end
end

function WorkStation:close_gui()
	print("WORKSTATION CLOSE GUI")
	self.gui_open = false
	--GuiManager:remove_popup(self.gui_id)
	self:gui_hide()
	Game.selected_construct = nil
	

end

function WorkStation:check_gui()

	return self.gui_open

end

function WorkStation:get_gui_id()

	return self.gui_id

end

function WorkStation:check_hidden()
	
	local s_gui = WorkStation.get_gui(self)
	
	if s_gui then
	
		return self.gui:getVisible()
	
	end
	

end

function WorkStation:get_gui()
	
	return self.gui
	

end

function WorkStation:gui_show()

	self.gui:show()

end

function WorkStation:gui_hide()

	self.gui:hide()

end

function WorkStation:set_gui_text( text )

	self.gui:setText(text)
	
end

function WorkStation:get_network_map()

	return self.network_map

end

function WorkStation:check_resources()
	
	if self.res then
		for i = 1, #self.res do
			
			if self.req[i] then
			
				return true
				
			end
		
		end
	end

end

function WorkStation:update_resources(dt)

	if dt > 1 then dt = 1/dt end
	
	local s, sp

	if self.res then
		
		for i = 1, #self.res do
			if self.res[i].current > self.res[i].min 
				and self.res[i].current <= self.res[i].max then
				local r = self.res[i].rate
				local c = self.res[i].current
				
				local form = r*(dt*TURN_TIME)
				
								
				c = c - form
				local a
			end
			
			if self.res[i].current > self.res[i].max then 
				self.res[i].current = self.res[i].max
			end
			
			if self.res[i].current < self.res[i].min then
				self.res[i].current = self.res[i].min
			end
			
			local res = self.res[i]
			if res.name and res.current then
				local rc = round(res.current, 2)
				s = " " .. res.name .. " " .. rc
				sp = self.type .. s
				
			end
			
			
			
		end
		
		local a
		
		local wgui = WorkStation.get_gui(self)
		if wgui.setText and sp then
			wgui:setText(sp)
		end
	
	end

end

function WorkStation:get_resources()

	local res_table = {}

	if self.res then
		for i = 1, #self.res do
			local iter = #res_table+1
			res_table[iter] = {}
			res_table[iter].current = self.res[i].current
			res_table[iter].name = self.res[i].name
			
		end
		
		return res_table
	end
	
	

end

function WorkStation:get_res()

	return self.res

end





return WorkStation


